package org.pisces.wechat.commons.button;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.conn.ConnectTimeoutException;
import org.pisces.wechat.commons.utils.HttpClientUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;


public class WchatUtils {


	
	public static void main(String[] args) throws ConnectTimeoutException, SocketTimeoutException, Exception {
	//	createTags();
//		batchtaggingMembers();
//		createMenu();
		getAccessToken();
	}
	
	
	
	public static void post(String path,String json) throws Exception{
		
//		String token = getAccessToken();
		String token ="E9thl13-4Sb9J435fC0F4s7k11P8O9NjNIPpdUGCARAKFSbn4HTvCH65dyYpTbA6eHlZJUHvEKkHjF-wBwUBizObc8TQAS5m3BJCskChtE83Ryn78RyKYQt9IHJ2xOmLLQXdABAXCJ";
		
		path+="?access_token="+token;
		URL url=new URL(path);
		HttpURLConnection http = (HttpURLConnection)url.openConnection();
		http.setDoOutput(true);
		http.setDoInput(true);
		http.setRequestMethod("POST");
		http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
		http.connect();
		OutputStream os = http.getOutputStream();
		os.write(json.getBytes("UTF-8"));
		os.close();
		
		InputStream is = http.getInputStream();
		int size = is.available();
		byte[] bt = new byte[size];
		is.read(bt);
		String message=new String(bt,"UTF-8");
		JSONObject jsonMsg = JSONObject.parseObject(message);
		System.out.println(jsonMsg);
	}
	
	/**
	 * 获取token
	 * @since 1.0 
	 * @return
	 * @throws Exception
	 * <br><b>作者： @author 宋旋 <xuan.song@utsoft.cn></b>
	 * <br>创建时间：2017年8月7日 下午5:32:31
	 */
	public static String  getAccessToken() throws Exception{
		String result = HttpClientUtils.get("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx5d36efd10bdba220&secret=44c83e520750fe80a5f3a64384b02a15");
		JSONObject json = JSONObject.parseObject(result);
		System.out.println(json);
		return json.getString("access_token");
	}
	
	/**
	 * 创建标签
	 * @since 1.0 
	 * @throws ConnectTimeoutException
	 * @throws SocketTimeoutException
	 * @throws Exception
	 * <br><b>作者： @author 宋旋 <xuan.song@utsoft.cn></b>
	 * <br>创建时间：2017年8月7日 下午5:32:38
	 */
	public static void  createTags() throws ConnectTimeoutException, SocketTimeoutException, Exception{
		JSONObject json = new JSONObject();
		JSONObject name = new JSONObject();
		name.put("name", "weixin");
		json.put("tag", name);
		post("http://api.weixin.qq.com/cgi-bin/tags/create", json.toJSONString());
	}
	
	
	
	/**
	 * 为用户打标签
	 * @throws Exception 
	 * @since 1.0 
	 * <br><b>作者： @author 宋旋 <xuan.song@utsoft.cn></b>
	 * <br>创建时间：2017年8月7日 下午5:35:25
	 */
	public static void batchtaggingMembers() throws Exception{
		JSONObject json = new JSONObject();
		JSONArray openidList = new JSONArray();
		openidList.add("ohaBbwuRjpxmqOu_nVnTviYT_zj8");
		json.put("tagid", 100);
		json.put("openid_list", openidList);
		post("http://api.weixin.qq.com/cgi-bin/tags/members/batchtagging", json.toJSONString());
	}
	
	public static void createMenu() throws Exception{
		JSONObject data = new JSONObject();
		Menu menu= new Menu();
		List<Button> button = new ArrayList<>();
		ClickButton clickButton = new ClickButton();
		clickButton.setKey("bind");
		clickButton.setName("QQ用户");
		button.add(clickButton);
		
		
		
		ViewButton viewButton = new ViewButton();
		viewButton.setUrl("http://www.lan-x.cn");
		viewButton.setName("QQ用户菜单");
		button.add(viewButton);
		
		
		ComplexButton complexButton = new ComplexButton();
		List<SimpleButton> sub_button = new ArrayList<>();
		complexButton.setName("更多帮助");
		ClickButton about = new ClickButton();
		about.setKey("about");
		about.setName("关于我们");
		sub_button.add(about);
		
		ClickButton cooperation  = new ClickButton();
		cooperation.setKey("cooperation");
		cooperation.setName("联系合作");
		sub_button.add(cooperation);
		
		
		ClickButton feedBack = new ClickButton();
		feedBack.setKey("feedBack");
		feedBack.setName("咨询反馈");
		sub_button.add(feedBack);
		
		
		complexButton.setSub_button(sub_button);
		button.add(complexButton);
		
		menu.setButton(button);
		data.put("button", button);
		
		JSONObject matchrule = new JSONObject();
		
		matchrule.put("tag_id", "100");
		data.put("matchrule", matchrule);
		
		post("http://api.weixin.qq.com/cgi-bin/menu/addconditional", data.toJSONString());
		
	}
	
}
